package mamikeji.liaowa.widget.clock.data;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.widget.clock.Model.AlarmModel;

/**
 *
 * Created by zyascend on 2016/3/4.
 */
public class MyAlarmDataBase {
    private SQLiteDatabase db;
    private MyAlarmOpenHelper myAlarmOpenHelper;

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = " LIAOWA_AlarmDatabase";
    private static final String TABLE_ALARM = " LIAOWA_AlarmTable";

    private static final String KEY_id = "id";
    private static final String KEY_usercode = "usercode";
    private static final String KEY_typeName = "typeName";
    private static final String KEY_timeGuid = "timeGuid";
    private static final String KEY_time = "time";
    private static final String KEY_week = "week";
    private static final String KEY_weekCode = "weekCode";
    private static final String KEY_mWakeType = "mWakeType";
    private static final String KEY_mActive = "mActive";
    private static final String KEY_mRing = "mRing";
    private static final String KEY_parentID = "parentID";

    public MyAlarmDataBase(Context context){
        MyAlarmOpenHelper openHelper = new MyAlarmOpenHelper(context,DATABASE_NAME,null,DATABASE_VERSION);
        this.myAlarmOpenHelper = openHelper;
        db=openHelper.getWritableDatabase();
    }

    public int addAlarm(AlarmModel alarmModel){
        ContentValues values = new ContentValues();

        values.put(KEY_typeName , alarmModel.getTypeName());
        values.put(KEY_timeGuid , alarmModel.getTimeGuid());
        values.put(KEY_time , alarmModel.getTime());
        values.put(KEY_week, alarmModel.getWeek());
        values.put(KEY_weekCode, alarmModel.getWeekCode());
        values.put(KEY_mWakeType , alarmModel.getmWakeType());
        values.put(KEY_mActive, alarmModel.getmActive());
        values.put(KEY_mRing,alarmModel.getmRing());
        values.put(KEY_parentID, alarmModel.getParentID());

        if (!db.isOpen()){
            db = myAlarmOpenHelper.getWritableDatabase();
        }

        long ID = db.insert(TABLE_ALARM,null,values);
        db.close();
        return (int)ID;
    }

    //获取单个闹钟
    public AlarmModel getAlarm(int id){
        if (!db.isOpen()){
            db = myAlarmOpenHelper.getWritableDatabase();
        }
        Cursor cursor = db.query(TABLE_ALARM,new String[]
                {
                        KEY_id,
                        KEY_usercode,
                        KEY_typeName,
                        KEY_timeGuid,
                        KEY_time,
                        KEY_week,
                        KEY_weekCode,
                        KEY_mWakeType,
                        KEY_mActive,
                        KEY_mRing,
                        KEY_parentID
                },KEY_id + "=?",new String[]{String.valueOf(id)},null,null,null,null);


        AlarmModel alarmModel = null;
        if (cursor != null && cursor.moveToFirst()) {

            alarmModel = new AlarmModel(Integer.parseInt(cursor.getString(0))
                    ,cursor.getString(1)
                    ,cursor.getString(2)
                    ,cursor.getString(3)
                    ,cursor.getString(4)
                    ,cursor.getString(5)
                    ,cursor.getString(6)
                    ,cursor.getString(7)
                    ,cursor.getString(8)
                    ,cursor.getString(9)
                    ,cursor.getString(10));

            cursor.close();
        }

        return alarmModel;
    }

    //根据父类ID获取
    public List<AlarmModel> getAlarmsList(String id) {
        List<AlarmModel> list = new ArrayList<>();
        if (!db.isOpen()) {
            db = myAlarmOpenHelper.getWritableDatabase();
        }
        Cursor cursor = db.query(TABLE_ALARM, null, KEY_parentID + "=?", new String[]{id}, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                AlarmModel alarmModel = new AlarmModel();
                alarmModel.setId(cursor.getInt(cursor.getColumnIndex(KEY_id)));
                alarmModel.setUsercode(cursor.getString(cursor.getColumnIndex(KEY_usercode)));
                alarmModel.setTypeName(cursor.getString(cursor.getColumnIndex(KEY_typeName)));
                alarmModel.setTimeGuid(cursor.getString(cursor.getColumnIndex(KEY_timeGuid)));
                alarmModel.setTime(cursor.getString(cursor.getColumnIndex(KEY_time)));
                alarmModel.setWeek(cursor.getString(cursor.getColumnIndex(KEY_week)));
                alarmModel.setWeekCode(cursor.getString(cursor.getColumnIndex(KEY_weekCode)));
                alarmModel.setmWakeType(cursor.getString(cursor.getColumnIndex(KEY_mWakeType)));
                alarmModel.setmActive(cursor.getString(cursor.getColumnIndex(KEY_mActive)));
                alarmModel.setmRing(cursor.getString(cursor.getColumnIndex(KEY_mRing)));
                alarmModel.setParentID(cursor.getString(cursor.getColumnIndex(KEY_parentID)));
                list.add(alarmModel);
            }
        }
        cursor.close();
        return list;
    }

    //获取所有Alarm
    public List<AlarmModel> getAllAlarms(){
        List<AlarmModel> list = new ArrayList<>();

        String selectQuery = "SELECT * FROM " + TABLE_ALARM;

        if (!db.isOpen()){
            db = myAlarmOpenHelper.getWritableDatabase();
        }

        Cursor cursor = db.rawQuery(selectQuery, null);

        if(cursor.moveToFirst()){
            do{
                AlarmModel alarmModel = new AlarmModel();
                alarmModel.setId(Integer.parseInt(cursor.getString(0)));
                alarmModel.setUsercode(cursor.getString(1));
                alarmModel.setTypeName(cursor.getString(2));
                alarmModel.setTimeGuid(cursor.getString(3));
                alarmModel.setTime(cursor.getString(4));
                alarmModel.setWeek(cursor.getString(5));
                alarmModel.setWeekCode(cursor.getString(6));
                alarmModel.setmWakeType(cursor.getString(7));
                alarmModel.setmActive(cursor.getString(8));
                alarmModel.setmRing(cursor.getString(9));
                alarmModel.setParentID(cursor.getString(10));

                list.add(alarmModel);
            } while (cursor.moveToNext());

        }
        cursor.close();
        return list;
    }


    public int updateAlarm(AlarmModel alarmModel){
        ContentValues values = new ContentValues();

        values.put(KEY_typeName , alarmModel.getTypeName());
        values.put(KEY_timeGuid , alarmModel.getTimeGuid());
        values.put(KEY_time , alarmModel.getTime());
        values.put(KEY_week, alarmModel.getWeek());
        values.put(KEY_weekCode, alarmModel.getWeekCode());
        values.put(KEY_mWakeType , alarmModel.getmWakeType());
        values.put(KEY_mActive, alarmModel.getmActive());
        values.put(KEY_mRing,alarmModel.getmRing());
        values.put(KEY_parentID, alarmModel.getParentID());

        if (!db.isOpen()){
            db = myAlarmOpenHelper.getWritableDatabase();
        }

        return db.update(TABLE_ALARM, values, KEY_id + "=?",
                new String[]{String.valueOf(alarmModel.getId())});
    }

    public void deleteAlarm(AlarmModel alarmModel){
        if (!db.isOpen()){
            db = myAlarmOpenHelper.getWritableDatabase();
        }
        db.delete(TABLE_ALARM, KEY_id + "=?",
                new String[]{String.valueOf(alarmModel.getId())});
        db.close();
    }
}
